import { AppStateType } from './types'
import { EnumType } from '@/components/global/enum/types'
import EnumModel from '@/models/EnumModel'
import ArticleModel from '@/models/ArticleModel'
import Cookies from 'js-cookie';

const PROJECT_ID = 'PROJECT_ID'

const initState: AppStateType = {
  enums: [],
  // 当前的项目
  project: undefined
}

// mutations
const mutations = {
  // 设置Enum
  SET_ENUMS: (state: AppStateType, enums: Array<EnumType>) => {
    state.enums = enums
  },
  // 设置项目
  SET_PROJECT: (state: AppStateType, project?: ArticleModel) => {
    if (project) {
      state.project = project
      state.enums = project.enums
      Cookies.set(PROJECT_ID, project._id)
    } else {
      state.project = undefined
      state.enums = []
      Cookies.remove(PROJECT_ID)
    }
  },
}

// actions
const actions = {
}

export default {
  state: initState,
  actions,
  mutations,
}
